package imp

import (
	sqlc "bgs/db/sqlc"
	userPvLogGrpc "bgs/grpc/gen/service/user_pv_log"
	"bgs/grpc/gen/shared/message"
	"context"
)

/****************************************************************************************/
/*                               user_pv_log entity of helper                                  */
/****************************************************************************************/

func transformUserPvLogEntityToPb(userPvLog sqlc.UserPvLog) *userPvLogGrpc.UserPvLogEntity {
	return &userPvLogGrpc.UserPvLogEntity{
		Id:          userPvLog.ID,
		Created:     sqlc.GoTimeToPBTimestamp(userPvLog.Created),
		UserId:      userPvLog.UserID,
		BrandId:     userPvLog.BrandID.Int64,
		ShareUserId: userPvLog.ShareUserID.Int64,
		Page:        userPvLog.Page,
	}
}

/****************************************************************************************/
/*                               user_pv_log entity of cmd                              */
/****************************************************************************************/

// CreateUserPvLog 创建用户pv日志
func (s *UserAPIService) CreateUserPvLog(ctx context.Context, req *userPvLogGrpc.CreateUserPvLogRequest) (res *userPvLogGrpc.CreateUserPvLogResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("user_pv_logImp->CreateUserPvLog:%v", req)
	res = &userPvLogGrpc.CreateUserPvLogResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	arg := sqlc.CreateUserPvLogParams{
		UserID:      req.UserId,
		BrandID:     sqlc.NInt64FromInt64(req.BrandId),
		ShareUserID: sqlc.NInt64FromInt64(req.ShareUserId),
		Page:        req.Page,
	}
	userPvLog, err := s.dao.Q.CreateUserPvLog(ctx, arg)
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.UserPvLog = transformUserPvLogEntityToPb(userPvLog)
	return
}

// UpdateUserPvLog 更新用户pv日志
func (s *UserAPIService) UpdateUserPvLog(ctx context.Context, req *userPvLogGrpc.UpdateUserPvLogRequest) (res *userPvLogGrpc.UpdateUserPvLogResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("user_pv_logImp->UpdateUserPvLog:%v", req)
	res = &userPvLogGrpc.UpdateUserPvLogResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	arg := sqlc.UpdateUserPvLogParams{
		ID:        req.Id,
		IsCounted: req.IsCounted,
	}
	userPvLog, err := s.dao.Q.UpdateUserPvLog(ctx, arg)
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.UserPvLog = transformUserPvLogEntityToPb(userPvLog)
	return
}

/****************************************************************************************/
/*                               user_pv_log entity of query                            */
/****************************************************************************************/

// GetLatestUserPvLogByUserID 根据用户ID获取最新的用户pv日志
func (s *UserAPIService) GetLatestUserPvLogByUserID(ctx context.Context, req *userPvLogGrpc.GetLatestUserPvLogByUserIdRequest) (res *userPvLogGrpc.GetLatestUserPvLogByUserIdResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("user_pv_logImp->GetLatestUserPvLogByUserID:%v", req)
	res = &userPvLogGrpc.GetLatestUserPvLogByUserIdResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	userPvLog, err := s.dao.Q.GetLatestPvLogByUserID(ctx, req.UserId)
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.UserPvLog = transformUserPvLogEntityToPb(userPvLog)
	return
}

// GetLatestUserPvLogByUserIDWithBrandID 根据用户和品牌ID获取最新的用户pv日志
func (s *UserAPIService) GetLatestUserPvLogByUserIDWithBrandID(ctx context.Context, req *userPvLogGrpc.GetLatestUserPvLogByUserIdWithBrandIdRequest) (res *userPvLogGrpc.GetLatestUserPvLogByUserIdWithBrandIdResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("user_pv_logImp->GetLatestUserPvLogByUserID:%v", req)
	res = &userPvLogGrpc.GetLatestUserPvLogByUserIdWithBrandIdResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	arg := sqlc.GetLatestPvLogByUserIDWithBrandIDParams{
		UserID:  req.UserId,
		BrandID: sqlc.NInt64FromInt64(req.BrandId),
	}
	userPvLog, err := s.dao.Q.GetLatestPvLogByUserIDWithBrandID(ctx, arg)
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.UserPvLog = transformUserPvLogEntityToPb(userPvLog)
	return
}

/****************************************************************************************/
/*                               user_pv_logAggs entity of helper                       */
/****************************************************************************************/

func transformAggsToUserRecentNewsEntityToPb(row sqlc.ListAggsToUserRecentNewsesByUserIDsRow) *userPvLogGrpc.AggsToUserRecentNewsEntity {
	return &userPvLogGrpc.AggsToUserRecentNewsEntity{
		UserId:     row.UserID,
		LatestTime: sqlc.GoTimeToPBTimestamp(row.LatestTime),
	}
}

func mapAggsToUserRecentNewsEntityToPb(list []sqlc.ListAggsToUserRecentNewsesByUserIDsRow, f func(sqlc.ListAggsToUserRecentNewsesByUserIDsRow) *userPvLogGrpc.AggsToUserRecentNewsEntity) []*userPvLogGrpc.AggsToUserRecentNewsEntity {
	r := make([]*userPvLogGrpc.AggsToUserRecentNewsEntity, len(list))
	for i, item := range list {
		r[i] = f(item)
	}
	return r
}

/****************************************************************************************/
/*                               user_pv_logAggs entity of query                        */
/****************************************************************************************/

// ListAggsToUserRecentNewsesByUserIDs  列出指定用户最新动态的聚合
func (s *UserAPIService) ListAggsToUserRecentNewsesByUserIDs(ctx context.Context, req *userPvLogGrpc.ListAggsToUserRecentNewsesByUserIDsRequest) (res *userPvLogGrpc.ListAggsToUserRecentNewsesByUserIDsResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("user_pv_logImp->ListAggsToUserRecentNewsesByUserIDs:%v", req)
	res = &userPvLogGrpc.ListAggsToUserRecentNewsesByUserIDsResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	aggsToUserRecentNewses, err := s.dao.Q.ListAggsToUserRecentNewsesByUserIDs(ctx, req.UserIds)
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.AggsToUserRecentNewses = mapAggsToUserRecentNewsEntityToPb(aggsToUserRecentNewses, transformAggsToUserRecentNewsEntityToPb)
	return
}
